from flask import Flask,request,render_template
import json
from MySQLHelper import MySQLHelper
from flask_cors import CORS
from SpeakModal import *
app = Flask(__name__,static_folder="static",template_folder="templates1")
CORS(app, resources=r'/*')
@app.route("/getUnivTagsDataForPie")
def getUnivTagsDataForPie():
    conn = MySQLHelper()
    sql="select univTags,count(*) as `count` from tb_univ GROUP BY univTags"
    rs=conn.executeForSelectAll(sql,())
    list1=[]
    for item in rs:
        dict1={}
        dict1["name"]=item[0]
        dict1["value"]=item[1]
        list1.append(dict1)
    print(list1)
    return json.dumps(list1,ensure_ascii=False)

@app.route("/findUnivList")
def findUnivList():
    limit=request.args.get("limit")
    page=request.args.get("page")
    conn = MySQLHelper();
    sql="select * from tb_univ limit %s,%s"
    rs=conn.executeForSelectAll(sql,[(int(page)-1)*int(limit),int(limit)])
    sql1="select count(*) as `count` from tb_univ"
    rs1=conn.executeForSelectAll(sql1,())
    count=0
    for i in rs1:
        count=tuple(i)[0]
    print("count=",count)
    listObject=[];
    for item in rs:
        dictObject={}
        print(item)
        dictObject["id"] = tuple(item)[0]
        dictObject["univNameCn"]=tuple(item)[1]
        dictObject["univNameEn"]=tuple(item)[2]
        dictObject["univLogo"]=tuple(item)[3]
        dictObject["univTags"]=tuple(item)[4]
        listObject.append(dictObject)

        myDictData={}
        myDictData["code"]=0
        myDictData["data"]=listObject
        myDictData["count"]=count
    return json.dumps(myDictData,ensure_ascii=False);


@app.route("/getUserDataForBar")
def getUserDataForBar():
    conn=MySQLHelper()
    sql="select case when static='1' then '启用' when static='0' then '禁用' end as '状态'," \
        "count(*) as `count` from tb_user GROUP BY static"
    rs=conn.executeForSelectAll(sql,())
    list1=[]
    list2=[]
    dict1={}
    for item in rs:
        list1.append(item[0])
        list2.append(item[1])
    dict1["enable"]=list1;
    dict1["count"]=list2;
    return json.dumps(dict1,ensure_ascii=False)

@app.route("/say")
def say():
    saycontent=robot()
    # print(saycontent)
    return saycontent

@app.route("/getUserInfo")
def getUserInfo():
    conn=MySQLHelper();
    sql="select * from tb_user limit %s,%s"
    rs=conn.executeForSelectAll(sql,(0,2))
    print(json.dumps(rs,ensure_ascii=False))
    return json.dumps(rs,ensure_ascii=False);

@app.route("/register",methods=["post"])
def register():
    '''register'''
    conn=MySQLHelper();
    sql="insert into tb_user values(null,%s,md5(%s),%s)"
    i=conn.executeForUCD(sql,[request.form.get("uname"),request.form.get("upwd"),0])
    if i>0 :
        return "1"
    else:
        return "0"


@app.route("/login",methods=["post"])
def login():
    '''这是个登录功能'''
    uname=request.form.get("l_uname","")
    upwd=request.form.get("l_upwd","")
    print(request.form.__str__())
    conn=MySQLHelper();
    sql="select * from tb_user where uname=%s and upwd=md5(%s)"
    rs=conn.executeForSelectAll(sql,(uname,upwd))
    i=rs.__len__()
    print(i)
    if i==0:
        return "0"#同步请求 fault
    else:
        return "1"  # success

@app.route("/manager")
def manager():
    return render_template("admin.html")

@app.route('/',methods=["get","post"])
def index():
    return render_template("index.html")

@app.route("/welcome")
def welcome():
    return  render_template("welcome.html")

@app.route("/articleList")
def articleList():
    return  render_template("articleList.html")

@app.route("/userList")
def userList():
    return  render_template("userList.html")

@app.route("/analysis")
def analysis():
    return  render_template("analysis.html")

@app.route("/univList")
def univList():
    return  render_template("univList.html")

@app.route("/addUnivInfo")
def addUnivInfo():
    return  render_template("addUnivInfo.html")

@app.route("/addUnivInfoData")
def addUnivInfoData():
    univCnname=request.args.get("univEnname",None)
    univEnname=request.args.get("univEnname",None)
    univTags=request.args.get("univTags",None)
    print(univCnname)
    conn=MySQLHelper()
    sql='insert into tb_univ(univNameCn,univNameEn,univLogo,univTags) values (%s,%s,%s,%s)'
    i=conn.executeForUCD(sql,[univCnname,univEnname,"logo/default.gif",univTags])
    return  str(i)

if __name__ == '__main__':
    app.debug=True
    app.run()
